package com.sh.贪心算法;

/**
 * @Auther: sh
 * @Version 1.0
 * @Description:
 */
public class 买卖股票的最佳时机 {
    public static void main(String[] args) {
        int prices[] = new int[]{7,1,5,3,6,4};
        int i = maxProfit(prices);
        System.out.println(i);
    }

    public static int maxProfit(int[] prices) {
        // 暴力算法
//        int res = 0;
//        for (int i = 0; i < prices.length; i++) {
//            for (int j = i + 1; j < prices.length; j++) {
//                res = Math.max(res, prices[j] - prices[i]);
//            }
//        }
//        return res;

        // 贪心算法
        int minPrices = prices[0];  //  初始化最低价
        int res = -1;
        for (int price : prices) {
            minPrices = Math.min(minPrices, price);
            res = Math.max(res, price-minPrices);
        }
        return res;

    }
}

